package org.chen.wordwave.act;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.hjq.toast.Toaster;

import org.chen.wordwave.R;
import org.chen.wordwave.base.BaseActivity;
import org.chen.wordwave.bean.Reservation;
import org.chen.wordwave.fragment.SouYeFragment;
import org.litepal.LitePal;

import java.util.List;

public class ReservationList extends BaseActivity {
    private ListView listView;
    private List<Reservation> reservationList;
    private ReservationAdapter adapter;
    private TextView tvNoReservations;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.reservation_list_act;
    }

    @Override
    public void init() {
        listView = findViewById(R.id.lv_reservations);
        tvNoReservations = findViewById(R.id.tv_no_reservations);
        reservationList = LitePal.findAll(Reservation.class);
        if (reservationList.isEmpty()) {
            listView.setVisibility(View.GONE);
            tvNoReservations.setVisibility(View.VISIBLE);
        } else {
            listView.setVisibility(View.VISIBLE);
            tvNoReservations.setVisibility(View.GONE);
            adapter = new ReservationAdapter();
            listView.setAdapter(adapter);
        }
    }

    private class ReservationAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return reservationList.size();
        }

        @Override
        public Object getItem(int position) {
            return reservationList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = getLayoutInflater().inflate(R.layout.item_reservation, parent, false);
            } else {
                view = convertView;
            }

            TextView tvLessonTitle = view.findViewById(R.id.tv_lesson_title);
            TextView tvLessonStart = view.findViewById(R.id.tv_lesson_start);
            TextView tvLessonTimes = view.findViewById(R.id.tv_lesson_times);
            Button btnCancelReservation = view.findViewById(R.id.btn_cancel_reservation);

            Reservation reservation = reservationList.get(position);

            tvLessonTitle.setText(reservation.getLessonTitle());
            tvLessonStart.setText(reservation.getLessonStart());
            tvLessonTimes.setText(reservation.getLessonTimes());

            btnCancelReservation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reservation.delete();
                    reservationList.remove(position);
                    notifyDataSetChanged();

                    Toaster.show(getString(R.string.APPOINTMENT_CANCELLED));

                    SouYeFragment.updateReservationStatus(reservation.getLessonTitle(), false);

                    // 检查是否还有预约
                    if (reservationList.isEmpty()) {
                        listView.setVisibility(View.GONE);
                        tvNoReservations.setVisibility(View.VISIBLE);
                    }
                }
            });

            return view;
        }
    }
}
